﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel;

namespace mscook.core.domain
{
    public class LoginUser : Entity
    {
        public LoginUser()
        {
            PurchasedThings = new Iesi.Collections.Generic.HashedSet<PurchasedThing>();
            VipInvalidDate = new DateTime(2000, 1, 1);
            IsAnonymous = true;
            Bonus = 300;
            TodayBonus = 20;
            UserRole = "admin";
            LastLogin = DateTime.Now.AddDays(1);
            //BonusCondition = new BonusCondition();

        }
        /// <summary>
        /// 用户名必填项，6-12位
        /// </summary>
        [Required(ErrorMessage = "用户名必填")]
        [StringLength(12, MinimumLength = 2, ErrorMessage = "用户名必须为2-12位")]

        public virtual string LoginName
        {
            get;

            set;

        }

        //public virtual BonusCondition BonusCondition
        //{
        //    set;
        //    get;
        //}

        public virtual DateTime LastLogin
        {
            set;
            get;
        }

        public virtual int TodayBonus
        {
            set;
            get;
        }
        /// <summary>
        /// 密码必填项，8-20位
        /// </summary>
        [Required(ErrorMessage = "密码必填")]
        [StringLength(20, MinimumLength = 8, ErrorMessage = "密码必须为8-20位")]
        public virtual string Password
        {
            get;

            set;

        }

        public virtual string ConfirmPassword
        {
            get;
            set;
        }
        /// <remarks>真实姓名</remarks>
        public virtual string Name
        {
            get;

            set;

        }
        ///<remarks>是否匿名用户</remarks>
        public virtual bool IsAnonymous
        {
            get;

            set;

        }

        [RegularExpression(@"^(\d{11})|^((\d{7,8})|(\d{4}|\d{3})-(\d{7,8})|(\d{4}|\d{3})-(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1})|(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1}))$", ErrorMessage = "请填写正确的联系方式")]
        public virtual string Tel
        {
            get;

            set;

        }


        /// <summary>
        /// 密码必填项，8-20位
        /// </summary>
        [Required(ErrorMessage = "邮箱必填")]
        [RegularExpression(@"^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$", ErrorMessage = "请填写正确格式的邮箱")]
        public virtual string Email
        {
            get;

            set;

        }

        public virtual string Address
        {
            set;
            get;
        }

        [RegularExpression(@"^([\w-]+\.)+[\w-]+(/[\w-./?%&=]*)?$", ErrorMessage = "请填写正确格式的网址")]
        public virtual string SelfPage
        {
            set;
            get;
        }


        public virtual string Description
        {
            get;

            set;

        }



        public virtual DateTime VipInvalidDate
        {
            get;
            set;
        }

        public virtual int Bonus
        {
            get;
            set;
        }

        public virtual string UserRole
        {
            get;
            set;
        }

        public virtual bool IsVip
        {
            get
            {
                return DateTime.Now < VipInvalidDate;
            }
        }
        public virtual Iesi.Collections.Generic.ISet<PurchasedThing> PurchasedThings
        {
            get;
            set;
        }

        public virtual IList<string> PurchasedThingIds(PurchasedCategory category)
        {
            return PurchasedThings.Where(o => o.PurchasedCategory == category && o.IsDelete == false).Select(o => o.RelatedId).ToList();
        }
        public virtual bool hasPurchased(string resourceId)
        {
            var item = PurchasedThings.Where(x => x.RelatedId == resourceId);
            return item.Any();

        }

        public virtual bool hasPurchased(string resourceId, string category)
        {
            var item = PurchasedThings.Where(
                x => x.RelatedId == resourceId
                && x.PurchasedCategory.ToString().Equals(category)
                );

            return item.Any();

        }

    }
}
